package cirno.open.api.service.impl;

import cirno.open.api.service.MailService;
import lombok.RequiredArgsConstructor;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Service;

import java.nio.charset.StandardCharsets;
import java.util.UUID;

@Service
@RequiredArgsConstructor
public class MailServiceImpl implements MailService {

    private final RabbitTemplate rabbitTemplate;

    @Override
    public void sendCaptcha(String emailAccount) {
        CorrelationData correlationId = new CorrelationData(UUID.randomUUID().toString());
        MessageProperties messageProperties = new MessageProperties();
        messageProperties.setExpiration("10000");
        Message message = new Message(emailAccount.getBytes(StandardCharsets.UTF_8), messageProperties);
        rabbitTemplate.send(
                "cirno.direct", "user.email.captcha",
                message,
                correlationId
        );
    }
}
